package com.xique.system.service.biz.service.impl;

import com.xique.common.core.constant.Constants;
import com.xique.common.core.constant.UserConstants;
import com.xique.common.core.domain.R;
import com.xique.common.core.utils.AddressUtils;
import com.xique.common.core.utils.DateUtils;
import com.xique.common.core.utils.ServletUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.utils.ip.IpUtils;
import com.xique.system.bean.domain.SystemLoginInfo;
import com.xique.system.bean.domain.SystemUser;
import com.xique.system.service.biz.service.ISystemLogBizService;
import com.xique.system.service.service.ISystemLoginInfoService;
import com.xique.system.service.service.ISystemUserService;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author shd
 * @create 2021/11/12 9:51
 */
@Service
public class SystemLogBizServiceImpl implements ISystemLogBizService {

    @Autowired
    private ISystemLoginInfoService systemLoginInfoService;

    @Autowired
    private ISystemUserService systemUserService;

    @Override
    public R<Boolean> saveLoginInfo(String username, String status, String message, String platform) {

        String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
        String address = AddressUtils.getRealAddressByIP(ip);
        final UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
        // 获取客户端操作系统
        String os = userAgent.getOperatingSystem().getName();
        // 获取客户端浏览器
        String browser = userAgent.getBrowser().getName();

        String onlineStatus = "";

        // 封装对象
        SystemLoginInfo loginInfo = new SystemLoginInfo();
        loginInfo.setLoginName(username);
        loginInfo.setIpaddr(ip);
        loginInfo.setMsg(message);
        loginInfo.setLoginLocation(address);
        loginInfo.setBrowser(browser);
        loginInfo.setOs(os);
        loginInfo.setPlatform(platform);
        // 日志状态
        if (Constants.LOGIN_SUCCESS.equals(status) || Constants.LOGOUT.equals(status))
        {
            loginInfo.setStatus("0");
            if (Constants.LOGIN_SUCCESS.equals(status)) {
                onlineStatus = UserConstants.OnlineStatus.ONLINE;
            } else {
                onlineStatus = UserConstants.OnlineStatus.OFFLINE;
            }
        }
        else if (Constants.LOGIN_FAIL.equals(status))
        {
            loginInfo.setStatus("1");
        }

        int count = systemLoginInfoService.insertLoginInfo(loginInfo);
        if (count <= 0) {
            return R.fail();
        }

        if (StringUtils.isNotEmpty(onlineStatus)) {
            SystemUser user = systemUserService.selectUserByUserName(username);
            if (StringUtils.isNotNull(user)) {
                SystemUser update = new SystemUser();
                update.setUserId(user.getUserId());
                update.setOnlineStatus(onlineStatus);
                systemUserService.updateById(update);
            }
        }

        return R.ok();
    }
}
